package com.hua.common.commonview.datepicker;

import android.content.Context;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.NumberPicker;

import com.hua.common.R;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;

/**
 * Created by huaxiaolin_pc on 2016/1/26.
 */
public class MyDatePicker extends LinearLayout implements NumberPicker.OnValueChangeListener {

    private ondateChangelistener listener = null;
    private NumberPicker numberPickerYear = null;
    private NumberPicker numberPickerMouth = null;
    private NumberPicker numberPickerDay = null;


    private int startYear = 1970;
    private int endYear = 1970;

    public MyDatePicker(Context context) {
        super(context);
        this.initViews(context);
    }

    public MyDatePicker(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.initViews(context);
    }

    public MyDatePicker(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.initViews(context);
    }

    public void setOnDateChangeListener(ondateChangelistener listener) {
        this.listener = listener;
    }

    public void setDate(int startYear, int endYear) {
        this.startYear = startYear;
        this.endYear = endYear;
        this.setNumberPickerYear();
        this.setNumberPickerMouth();
        this.setNumberPickerDay();
        this.setNumberPickerOnchangeListener();
    }

    private void setNumberPickerOnchangeListener() {
        this.numberPickerYear.setOnValueChangedListener(this);
        this.numberPickerMouth.setOnValueChangedListener(this);
    }


    public void setNumberPickerYear() {
        this.numberPickerYear.setMaxValue(endYear);
        this.numberPickerYear.setMinValue(startYear);
    }

    public void setNumberPickerMouth() {
        this.numberPickerMouth.setMaxValue(12);
        this.numberPickerMouth.setMinValue(1);
    }

    public void setNumberPickerDay() {
        this.numberPickerDay.setMaxValue(getDays(numberPickerYear.getValue() + "", numberPickerMouth.getValue() + ""));
        this.numberPickerDay.setMinValue(1);
    }

    /**
     * 初始化控件
     */
    private void initViews(Context context) {
        this.setGravity(Gravity.CENTER_HORIZONTAL);
        LayoutInflater inflater = LayoutInflater.from(context);
        View view = inflater.inflate(R.layout.datepicker_layout, null);
        this.numberPickerYear = (NumberPicker) view.findViewById(R.id.year);
        this.numberPickerMouth = (NumberPicker) view.findViewById(R.id.mouth);
        this.numberPickerDay = (NumberPicker) view.findViewById(R.id.day);
        numberPickerYear.setDescendantFocusability(NumberPicker.FOCUS_BLOCK_DESCENDANTS);
        numberPickerMouth.setDescendantFocusability(NumberPicker.FOCUS_BLOCK_DESCENDANTS);
        numberPickerDay.setDescendantFocusability(NumberPicker.FOCUS_BLOCK_DESCENDANTS);
        this.addView(view);
        this.setNumberPickerYear();
        this.setNumberPickerMouth();
        this.setNumberPickerDay();
        this.setNumberPickerOnchangeListener();
    }


    /**
     * 返回开始日期的天数
     *
     * @return
     */
    private int getDays(String year, String mouth) {
        Calendar calendar = Calendar.getInstance();
        SimpleDateFormat simpleDate = new SimpleDateFormat("yyyy/MM");
        try {
            calendar.setTime(simpleDate.parse(year + "/" + mouth));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        int maxDays = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);//根据年月 获取月份天数
        return maxDays;
    }

    @Override
    public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
        if (picker.getId() == R.id.year || picker.getId() == R.id.mouth) {
            this.setNumberPickerDay();
        }
        if (this.listener != null) {
            int year = this.numberPickerYear.getValue();
            int mouth = this.numberPickerMouth.getValue();
            int day = this.numberPickerDay.getValue();
            listener.ondateChange(year, mouth, day);
        }
    }
}
